package com.lp.multi.thread.example04;

import java.awt.*;

/**
 * @author: lp
 * @create: 2021-06-21 15:17
 * @description: 通过标志位暂停线程
 */
public class StopThread implements Runnable{
    private  boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while (flag){
                System.out.println("i = " + i++);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        StopThread stopThread = new StopThread();
        new Thread(stopThread).start();
        for (int i = 0; i < 100; i++) {
            Thread.sleep(1);
            if (i==20) {
                stopThread.stop();
                System.out.println("主线程i   = " + i + ",stopThread线程被停止");
            }
        }

    }

    public void stop(){
        this.flag = false;
    }
}
